package cn.com.ege.mvc.action.business;

import java.io.Serializable;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.ege.mvc.controller.BaseAction;
import cn.com.ege.mvc.controller.IAction;
import cn.com.ege.mvc.exception.BusinessException;
import cn.com.ege.mvc.model.business.Client;
import cn.com.ege.mvc.service.impl.business.ClientService;
import cn.com.ege.mvc.util.PageController;

public class ClientAction extends BaseAction implements IAction, Serializable {
	private final static Log logger = LogFactory
			.getLog(ClientAction.class);
	private PageController pager = null;
	private Client client = null;
	private ClientService clientService = null;
	private List<Client> clientList = null;
	
	public String execute(HttpServletRequest request,
			HttpServletResponse response, ServletConfig servletConfig) {
		String act = request.getParameter("act");
		if (null != act) {
			if ("list".equals(act)) {
				// 取列表
				if ("list".equals(act)) {
					int totalRows = clientService.getRecordCount(new Client());
					String URL = request.getRequestURI();
					this.pager.setURL(URL);
					this.pager.setTotalRowsAmount(totalRows);
					clientList = clientService
							.list(new Client(), this.pager.getPageStartRow(),
									pager.getPageSize(), "order by id desc", true);
					pager.setData(clientList);
					return getReturnURL(LIST);
				}
			}
			// 点了添加或着点了修改
			else if ("get".equals(act)) {
				client = clientService.get(client);

				return getReturnURL(EDIT);
			}
			// 保存表单
			else if ("save".equals(act)) {
				try {
					clientService.save(client);
					return getReturnURL("getList");
				} catch (BusinessException e) {
					// 保存原来表单已输入的内容
					request.setAttribute("client", client);
					addMessage(e.getMessage());
					return getReturnURL(EDIT);
				}
			}
			// 删除
			else if ("del".equals(act)) {
				String[] ids = request.getParameterValues("id");
				for (String clientId : ids) {
					Client sp = new Client();
					if (null != clientId && !"0".equals(clientId)) {
						sp.setId(Long.parseLong(clientId));
						clientService.delete(sp);
					}
				}
				return getReturnURL("getList");
			}
		}
		return null;
	}

	public PageController getPager() {
		return pager;
	}

	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public ClientService getClientService() {
		return clientService;
	}

	public void setClientService(ClientService clientService) {
		this.clientService = clientService;
	}

	public List<Client> getClientList() {
		return clientList;
	}

	public void setClientList(List<Client> clientList) {
		this.clientList = clientList;
	}

	

}
